package com.ruoyi.web.controller.basice_info;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.basice_info.domain.TbSemester;
import com.ruoyi.basice_info.service.ITbSemesterService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学期设置Controller
 *
 * @author hongyi
 * @date 2023-12-07
 */
@RestController
@RequestMapping("/basice_info/semester")
public class TbSemesterController extends BaseController
{
    @Autowired
    private ITbSemesterService tbSemesterService;

    /**
     * 查询学期设置列表
     */
    @PreAuthorize("@ss.hasPermi('basice_info:semester:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbSemester tbSemester)
    {
        startPage();
        List<TbSemester> list = tbSemesterService.selectTbSemesterList(tbSemester);
        return getDataTable(list);
    }

    /**
     * 导出学期设置列表
     */
    @PreAuthorize("@ss.hasPermi('basice_info:semester:export')")
    @Log(title = "学期设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbSemester tbSemester)
    {
        List<TbSemester> list = tbSemesterService.selectTbSemesterList(tbSemester);
        ExcelUtil<TbSemester> util = new ExcelUtil<TbSemester>(TbSemester.class);
        util.exportExcel(response, list, "学期设置数据");
    }

    /**
     * 获取学期设置详细信息
     */
    @PreAuthorize("@ss.hasPermi('basice_info:semester:query')")
    @GetMapping(value = "/{semesterId}")
    public AjaxResult getInfo(@PathVariable("semesterId") Long semesterId)
    {
        return success(tbSemesterService.selectTbSemesterBySemesterId(semesterId));
    }

    /**
     * 新增学期设置
     */
    @PreAuthorize("@ss.hasPermi('basice_info:semester:add')")
    @Log(title = "学期设置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbSemester tbSemester)
    {
        addOrEdit(tbSemester,"add");
        return toAjax(tbSemesterService.insertTbSemester(tbSemester));
    }



    /**
     * 修改学期设置
     */
    @PreAuthorize("@ss.hasPermi('basice_info:semester:edit')")
    @Log(title = "学期设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbSemester tbSemester)
    {    addOrEdit(tbSemester,"edit");
        return toAjax(tbSemesterService.updateTbSemester(tbSemester));
    }


    public void  addOrEdit(TbSemester tbSemester, String type) {
        TbSemester semesterCode = tbSemesterService.getTbSemesterBySemesterByCode(tbSemester.getSemesterCode());
        TbSemester semesterName = tbSemesterService.getTbSemesterBySemesterByName(tbSemester.getSemesterName());
            if(semesterCode !=null) {
                if(type.equals("add")) {
                    throw new IllegalArgumentException("学期代码已经存在");
                } else {
                    if (!tbSemester.getSemesterId().equals(semesterCode.getSemesterId())) {
                        throw new IllegalArgumentException("学期代码已经存在");
                    }
                }
            }
            if (semesterName !=null) {
                if (type.equals("add")) {
                    throw new IllegalArgumentException("学期名称已经存在");
                } else {
                    if (!tbSemester.getSemesterId().equals(semesterName.getSemesterId())) {
                        throw new IllegalArgumentException("学期名称已经存在");
                    }
                }
            }
    }

    /**
     * 删除学期设置
     */
    @PreAuthorize("@ss.hasPermi('basice_info:semester:remove')")
    @Log(title = "学期设置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{semesterIds}")
    public AjaxResult remove(@PathVariable Long[] semesterIds)
    {
        return toAjax(tbSemesterService.deleteTbSemesterBySemesterIds(semesterIds));
    }
}
